package com.yungu.swift.system.marketing.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.marketing.dao.MarketingCouponCityMapper;
import com.yungu.swift.system.marketing.dao.MarketingCouponMapper;
import com.yungu.swift.system.marketing.model.dto.MarketingCouponCityDto;
import com.yungu.swift.system.marketing.model.dto.MarketingCouponDto;
import com.yungu.swift.system.marketing.model.dto.MarketingGiftDto;
import com.yungu.swift.system.marketing.service.MarketingCouponService;
import com.yungu.swift.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠券管理业务逻辑处理接口实现类
 * 重写add 方法
 *
 * @author xiuyin.cui - Generate
 * @version 1.1.0-SNAPSHOT
 * @since 2019-04-25 13:50
 */
@Service
public class MarketingCouponServiceImpl extends BaseServiceImpl<MarketingCouponDto> implements MarketingCouponService {
    @Autowired
    private MarketingCouponMapper marketingCouponMapper;
    @Autowired
    private MarketingCouponCityMapper marketingCouponCityMapper;

    @Override
    protected IMapper<MarketingCouponDto> getMapper() {
        return marketingCouponMapper;
    }

    @Override
    public ResponseData<List<MarketingCouponDto>> listExtend(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(marketingCouponMapper.listExtend(params));
    }

    @Override
    public ResponseData<Boolean> add(MarketingCouponDto marketingCouponDto) {
        String couponUuid = StringUtils.buildUUID();
        marketingCouponDto.setUuid(couponUuid);
        marketingCouponDto.setCreateOn(new Date());
        int affectedRowCount = this.getMapper().add(marketingCouponDto);
        if (1 == affectedRowCount) {
            //批量新增适用城市
            String[] cityUuidArray = marketingCouponDto.getCityUuids().split(",");
            List<MarketingCouponCityDto> list = new ArrayList<>();
            for (String cityUuid : cityUuidArray) {
                MarketingCouponCityDto marketingCouponCityDto = new MarketingCouponCityDto();
                marketingCouponCityDto.setUuid(StringUtils.buildUUID());
                marketingCouponCityDto.setCouponUuid(couponUuid);
                marketingCouponCityDto.setCityUuid(cityUuid);
                marketingCouponCityDto.setCreateBy(marketingCouponDto.getCreateBy());
                marketingCouponCityDto.setCreateOn(new Date());
                list.add(marketingCouponCityDto);
            }
            if (!list.isEmpty()) {
                marketingCouponCityMapper.addBatch(list);
            }
            return ResponseData.buildSuccessResponse(Boolean.TRUE);
        }
        return ResponseData.buildSuccessResponse(Boolean.FALSE);
    }

    @Override
    public ResponseData<List<MarketingCouponDto>> listByGiftUuid(String giftUuid) {
        return ResponseData.buildSuccessResponse(this.marketingCouponMapper.listByGiftUuid(giftUuid));
    }

    @Override
    public ResponseData<List<MarketingCouponDto>> getActiveCouponsByGift(MarketingGiftDto marketingGiftDto) {
        return ResponseData.buildSuccessResponse(this.marketingCouponMapper.getActiveCouponsByGift(marketingGiftDto));
    }

    @Override
    public ResponseData<BigDecimal> getSumMoneyByTypes(List<Integer> types) {
        return ResponseData.buildSuccessResponse(this.marketingCouponMapper.getSumMoneyByTypes(types, null));
    }

    @Override
    public ResponseData<BigDecimal> getSumMoneyByTypes(List<Integer> types, Integer getMode) {
        return ResponseData.buildSuccessResponse(this.marketingCouponMapper.getSumMoneyByTypes(types, getMode));
    }

    @Override
    public ResponseData<BigDecimal> getSumMoney(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(this.marketingCouponMapper.getSumMoney(params));
    }

    @Override
    public ResponseData<List<MarketingCouponDto>> getActiveCouponsByMap(Map<String, Object> params) {
        return ResponseData.buildSuccessResponse(this.marketingCouponMapper.getActiveCouponsByMap(params));
    }
}